/**
 * Created by .
 * User: Thomas Kurz
 * Date: 18.02.11
 * Time: 18:46
 * To change this template use File | Settings | File Templates.
 */
$.kiwi2 = $.kiwi2 || {};
$.widget("kiwi2.statistics", {
    /**
     * is called when widget gets initialized
     */
    _init: function(){
        this.initContainer();
        this.initDialog();
        this.writeContent();
    },

    /**
     * build html environment for the widget
     */
    initContainer: function() {
        var that = this;
        var widget = $("<div/>",{"class":"widget ui-widget"});
        this.contentBox = $("<div/>",{"class":"widget-content ui-widget-content"});
        var headerBox = $("<div/>",{"class":"widget-header ui-widget-header"});
        var header = $("<h3/>");
        var buttons = $("<div/>",{"style":"float:right;margin-top:-18px;margin-right:2px;"});
        this.blinder = $("<button/>",{"class":"small-button"});
        this.blinder.text(this.options.labels.showhide[this.options.lang])
        this.blinder.button({icons: {primary: "ui-icon-minus"},text:false});
        this.blinder.click(function(){
            that.hideContent();
        });
        buttons.append(this.blinder);
        header.text(this.options.labels.title[this.options.lang]);
        this.element.html("");
        widget.appendTo(this.element);
        headerBox.appendTo(widget);
        this.contentBox.appendTo(widget);
        header.appendTo(headerBox);
        buttons.appendTo(headerBox);
        this.contentBox1 = $("<div style='text-align:right'/>");
        this.contentBox2 = $("<div/>");
        this.contentBox2.text(this.options.labels.loading[this.options.lang]);
        this.contentBox.append(this.contentBox1);
        this.contentBox.append(this.contentBox2);
    },

    /**
     * build a html environment for the dialog
     */
    initDialog: function() {
        this.dialog = $("<div style='display:none'/>");
        this.dialog.appendTo(this.element);
    },

    writeContent : function() {
        this.writeContent1();
        this.writeContent2();
    },

    writeContent2 : function() {
        var that = this;
        var url = this.options.host+"/statistics";
        $.getJSON(url+"/json", function(data) {
            that.contentBox2.html("");
            for (property in data) {
                that.writeParagraph(property,data[property]);
            }
        });
    },

    writeContent1 : function() {
        var that = this;
        var url = this.options.host+"/statistics";
        $.getJSON(url+"/enabled", function(data) {
            that.writeBox2(data);
        });
    },

    writeBox2 : function(data) {
        var that = this;
        this.contentBox1.html("");
        var button1 = $("<button/>").click(function(){
           that.writeContent2();
        });
        button1.text(this.options.labels.reload[this.options.lang]);
        button1.button({icons: {primary: "ui-icon-play"},text:true});
        this.contentBox1.append(button1);
        if(data) {
           var button2 = $("<button/>").click(function(){
                that.endisable(false);
           });
           button2.text(this.options.labels.disable[this.options.lang]);
           button2.button({icons: {primary: "ui-icon-cancel"},text:true});
           this.contentBox1.append(button2);
        } else {
           var button2 = $("<button/>").click(function(){
              that.endisable(true);
           });
           button2.text(this.options.labels.enable[this.options.lang]);
           button2.button({icons: {primary: "ui-icon-check"},text:true});
           this.contentBox1.append(button2);
        }
    },

    endisable : function(data) {
        var that = this;
        $.ajax({
            type:"PUT",
            contentType: "application/json",
            url: this.options.host+"/statistics/enabled?value="+data,
            success: function(result) {
                that.writeContent();
            },
            error: function(jXHR) {
                alert(jXHR.statusText);
            }
        });
    },

    writeParagraph : function(property,data) {
        var fieldset = $("<fieldset/>");
        fieldset.html("<legend>"+property+"</legend>");
        var table = $("<table/>");
        for(property in data) {
            table.append("<tr><td>"+property+"</td><td>"+data[property]+"</td></tr>");
        }
        fieldset.append(table);
        this.contentBox2.append(fieldset);
    },

    /**
     * if widget is hidden, then show it and vice versa
     */
    hideContent: function() {
        var that = this;
        if(this.contentBox.is(":visible")) {
            this.contentBox.hide("blind",function(){
                that.blinder.button({icons: {primary:"ui-icon-plus"}});
            });
        }
        else {
            this.contentBox.show("blind",function(){
                that.blinder.button({icons: {primary:"ui-icon-minus"}});
            })
        }
    },

    /**
     * this are the options that customize the widget
     */
    options: {
	    host: 'http://localhost:8080/KiWi2',
        labels: {"title":{"de":"Statistik","en":"Statistics"},
                 "loading":{"de":"Laden...","en":"Loading..."},
                 "enable":{"de":"Aktivieren","en":"Enable"},
                 "disable":{"de":"Deaktivieren","en":"Disable"},
                 "reload":{"de":"Aktualisieren","en":"Reload"},
                 "showhide":{"de":"Aus-/Einblenden","en":"Show/Hide"}},
        lang: "de"
    }
});
